package com.triones.ljr.controller.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PdfUtil;
import com.triones.ljr.entity.dto.HandleAppDO;
import com.triones.ljr.entity.dto.HandleCategorizeDO;
import com.triones.ljr.entity.vo.CustomHandleAppVO;
import com.triones.ljr.entity.vo.HandleCategorizeVO;
import com.triones.ljr.service.HandleAppService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;

/**
 * 办理应用
 *
 * @Author: triones
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/handleApp")
public class HandleAppController extends BaseController {

    private final HandleAppService handleAppService;

    /**
     * 分类-查询列表（分页）
     */
    @SaCheckPermission("system:handleCategorize:pageList")
    @GetMapping("/categorize/pageList")
    public TableDataInfo<HandleCategorizeVO> categorizePageList(HandleCategorizeDO dto,PageQuery pageQuery) {
        return handleAppService.categorizePageList(dto,pageQuery);
    }

    /**
     * 分类-查询详细
     * @param id
     */
    @GetMapping(value = "/categorize/{id}")
    public R<HandleCategorizeVO> getCategorizeInfo(@PathVariable Long id) {
        return R.ok(handleAppService.getCategorizeInfo(id));
    }

    /**
     * 分类-新增
     */
    @SaCheckPermission("system:handleCategorize:add")
    @Log(title = "办理应用-分类", businessType = BusinessType.INSERT)
    @PostMapping("/categorize")
    public R<Void> addCategorize(@Validated @RequestBody HandleCategorizeDO dto) {
        if (!handleAppService.checkCategorizeUnique(dto)) {
            return R.fail("新增分类'" + dto.getName() + "'失败，分类已存在");
        }
        return toAjax(handleAppService.addCategorize(dto));
    }

    /**
     * 分类-修改
     */
    @SaCheckPermission("system:handleCategorize:edit")
    @Log(title = "办理应用-分类", businessType = BusinessType.INSERT)
    @PutMapping("/categorize")
    public R<Void> editCategorize(@Validated @RequestBody HandleCategorizeDO dto) {
        if (!handleAppService.checkCategorizeUnique(dto)) {
            return R.fail("修改分类'" + dto.getName() + "'失败，分类已存在");
        }
        return toAjax(handleAppService.editCategorize(dto));
    }

    /**
     * 分类-删除
     * @param id
     */
    @SaCheckPermission("system:handleCategorize:remove")
    @Log(title = "办理应用-分类", businessType = BusinessType.INSERT)
    @DeleteMapping("/categorize/{id}")
    public R<Void> removeCategorize(@PathVariable Long id) {
        return toAjax(handleAppService.removeCategorize(id));
    }

    /**
     * 分类-禁用/启用
     */
    @SaCheckPermission("system:handleCategorize:changeStatus")
    @Log(title = "办理应用-分类", businessType = BusinessType.UPDATE)
    @PutMapping("/categorize/changeStatus")
    public R<Void> categorizeChangeStatus(@RequestBody HandleCategorizeDO dto) {
        return toAjax(handleAppService.categorizeChangeStatus(dto));
    }

    /**
     * 获取选择框列表
     */
    @GetMapping("/categorize/optionSelect")
    public R<List<HandleCategorizeVO>> optionSelect() {
        List<HandleCategorizeVO> list = handleAppService.selectCategorizeAll(null);
        return R.ok(list);
    }

    /**
     * 查询列表（分页）
     */
    @SaCheckPermission("system:handleApp:pageList")
    @GetMapping("/pageList")
    public TableDataInfo<CustomHandleAppVO> pageList(HandleAppDO dto, PageQuery pageQuery) {
        return handleAppService.pageList(dto, pageQuery);
    }

    /**
     * 查询详细
     * @param id
     */
    @SaCheckPermission("system:handleApp:query")
    @GetMapping(value = "/{id}")
    public R<CustomHandleAppVO> getInfo(@PathVariable Long id) {
        return R.ok(handleAppService.getInfo(id));
    }

    /**
     * 新增
     */
    @SaCheckPermission("system:handleApp:add")
    @Log(title = "办理应用", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody HandleAppDO dto) {
//        if (!handleAppService.checkNameUnique(dto)) {
//            return R.fail("新增应用'" + dto.getName() + "'失败，应用名称已存在");
//        }
        return toAjax(handleAppService.add(dto));
    }

    /**
     * 修改
     */
    @SaCheckPermission("system:handleApp:edit")
    @Log(title = "办理应用", businessType = BusinessType.INSERT)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody HandleAppDO dto) {
//        if (!handleAppService.checkNameUnique(dto)) {
//            return R.fail("修改应用'" + dto.getName() + "'失败，应用名称已存在");
//        }
        return toAjax(handleAppService.edit(dto));
    }

    /**
     * 删除
     * @param id
     */
    @SaCheckPermission("system:handleApp:remove")
    @Log(title = "办理应用", businessType = BusinessType.INSERT)
    @DeleteMapping("/{id}")
    public R<Void> remove(@PathVariable Long id) {
        return toAjax(handleAppService.remove(id));
    }
}
